package com.hcbd.tdwt.ui.xingyu.adapter;

import android.content.Context;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.SectionIndexer;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.hcbd.tdwt.MainApp;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.xingyu.vo.BoxContact;

import java.util.List;


/**
 * - @Description:  获取联系人列表适配器
 * - @Author:  LXJ
 * - @Time:  2018/12/5 17:43
 */

public class AddressBookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements SectionIndexer {
    private String TAG = "AddressBookAdapter";
    private Context mContext;
    private List<BoxContact> mList;
    private LayoutInflater mLayoutInflater;
    //搜索框内容
    private String searchingString = "";
    //是否搜索模式
    private boolean isSearchType = false;

    //有索引
    private static final int ITEM_HAS_LETTER = 2;
    //没有索引
    private static final int ITEM_WITHOUT_LETTER = 3;

    public AddressBookAdapter(Context context, List<BoxContact> list, OnItemClickListener onItemClickListener) {
        this.mContext = context;
        this.mList = list;
        this.mOnItemClickListener = onItemClickListener;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_HAS_LETTER) {
            return new MyViewHolder1(mLayoutInflater.inflate(R.layout.contact_has_letter_item, parent, false));
        } else {
            return new MyViewHolder2(mLayoutInflater.inflate(R.layout.contact_without_letter_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        BoxContact user = mList.get(position);
        String boxid=user.getBoxId();
        if (holder instanceof MyViewHolder1) {
            //设置昵称
            if (user.getName() == null || user.getName().equals("")) {
                ((MyViewHolder1) holder).contactNameTv.setText(boxid);
                ((MyViewHolder1) holder).contactHeadImg.setText(boxid.substring(0, 1));
                if (!TextUtils.isEmpty(searchingString)) {
                    int idPosition = boxid.indexOf(searchingString);
                    if (boxid.contains(searchingString)) {
                        SpannableStringBuilder builder = new SpannableStringBuilder(boxid);
                        ForegroundColorSpan colorSpan = new ForegroundColorSpan(MainApp.getInstance()
                                .getResources().getColor(R.color.red));
                        builder.setSpan(colorSpan, idPosition, idPosition + searchingString.length(),
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ((MyViewHolder1) holder).contactNameTv.setText(builder);
                    }
                }
            } else {
                String name = user.getName();
                ((MyViewHolder1) holder).contactNameTv.setText(name);
                ((MyViewHolder1) holder).contactHeadImg.setText(name.substring(0, 1));
                if (!TextUtils.isEmpty(searchingString)) {
                    int idPosition = name.indexOf(searchingString);
                    if (name.contains(searchingString)) {
                        SpannableStringBuilder builder = new SpannableStringBuilder(name);
                        ForegroundColorSpan colorSpan = new ForegroundColorSpan(MainApp.getInstance()
                                .getResources().getColor(R.color.red));
                        builder.setSpan(colorSpan, idPosition, idPosition + searchingString.length(),
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ((MyViewHolder1) holder).contactNameTv.setText(builder);
                    }
                }
            }
            //设置卡号
            ((MyViewHolder1) holder).contactCardIdTv.setText(boxid);
            if (isSearchType) {
                ((MyViewHolder1) holder).contactCardIdTv.setVisibility(View.VISIBLE);
            } else {
                ((MyViewHolder1) holder).contactCardIdTv.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(searchingString)) {
                int idPosition = boxid.indexOf(searchingString);
                if (boxid.contains(searchingString)) {
                    SpannableStringBuilder builder = new SpannableStringBuilder(boxid);
                    ForegroundColorSpan colorSpan = new ForegroundColorSpan(MainApp.getInstance()
                            .getResources().getColor(R.color.red));
                    builder.setSpan(colorSpan, idPosition, idPosition + searchingString.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ((MyViewHolder1) holder).contactCardIdTv.setText(builder);
                }
            }
            //设置首字母
            if (!TextUtils.isEmpty(user.getSortLetter())) {
                ((MyViewHolder1) holder).itemContactFirstLetter.setText(user.getSortLetter());
            }
            //设置点击事件
            ((MyViewHolder1) holder).contactItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.OnItemClickListener(v, position);
                }
            });
        } else if (holder instanceof MyViewHolder2) {
            if (user.getName() == null || user.getName().equals("")) {
                ((MyViewHolder2) holder).contactNameTv.setText(boxid);
                ((MyViewHolder2) holder).contactHeadImg.setText(boxid.substring(0, 1));
                if (!TextUtils.isEmpty(searchingString)) {
                    int idPosition = boxid.indexOf(searchingString);
                    if (boxid.contains(searchingString)) {
                        SpannableStringBuilder builder = new SpannableStringBuilder(boxid);
                        ForegroundColorSpan colorSpan = new ForegroundColorSpan(MainApp.getInstance()
                                .getResources().getColor(R.color.red));
                        builder.setSpan(colorSpan, idPosition, idPosition + searchingString.length(),
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ((MyViewHolder2) holder).contactNameTv.setText(builder);
                    }
                }
            } else {
                String name=user.getName();
                ((MyViewHolder2) holder).contactNameTv.setText(name);
                ((MyViewHolder2) holder).contactHeadImg.setText(name.substring(0, 1));
                if (!TextUtils.isEmpty(searchingString)) {
                    int idPosition = name.indexOf(searchingString);
                    if (name.contains(searchingString)) {
                        SpannableStringBuilder builder = new SpannableStringBuilder(name);
                        ForegroundColorSpan colorSpan = new ForegroundColorSpan(MainApp.getInstance()
                                .getResources().getColor(R.color.red));
                        builder.setSpan(colorSpan, idPosition, idPosition + searchingString.length(),
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ((MyViewHolder2) holder).contactNameTv.setText(builder);
                    }
                }
            }
            //设置卡号
            ((MyViewHolder2) holder).contactCardIdTv.setText(user.getBoxId());
            if (isSearchType) {
                ((MyViewHolder2) holder).contactCardIdTv.setVisibility(View.VISIBLE);
            } else {
                ((MyViewHolder2) holder).contactCardIdTv.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(searchingString)) {
                int idPosition = boxid.indexOf(searchingString);
                if (boxid.contains(searchingString)) {
                    //Log.i("InfoMessage",name+"searchingString后:"+searchingString);
                    SpannableStringBuilder builder = new SpannableStringBuilder(boxid);
                    ForegroundColorSpan colorSpan = new ForegroundColorSpan(MainApp.getInstance()
                            .getResources().getColor(R.color.red));
                    builder.setSpan(colorSpan, idPosition, idPosition + searchingString.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ((MyViewHolder2) holder).contactCardIdTv.setText(builder);
                }
            }
            //设置点击事件
            ((MyViewHolder2) holder).contactItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.OnItemClickListener(v, position);
                }
            });
        }
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.e(TAG, "长按");
                popupMenu = new PopupMenu(mContext,v);
                popupMenu.getMenuInflater().inflate(R.menu.chat_menu,popupMenu.getMenu());
                //弹出式菜单的菜单项点击事件
                popupMenu.setOnMenuItemClickListener(item -> {
                    initPopupMenu(item,position);
                    return false;
                });
                popupMenu.show();
//                popupMenu.setOnDismissListener(popupMenu -> {
//                    v.setBackground(touchPreDrawable);
//                });
                return false;
            }
        });

    }
    PopupMenu popupMenu;
    public void initPopupMenu(MenuItem item, int position){
        if(item.getTitle().toString().equals("删除")){
            mOnItemClickListener.OnDelClickListener(position);
        }else if(item.getTitle().toString().equals("编辑")){
            mOnItemClickListener.OnEditClickListener(position);
        }
    }

    @Override
    public int getItemViewType(int position) {
        int type;
        int section = getSectionForPosition(position);
        if (position == getPositionForSection(section)) {
            type = ITEM_HAS_LETTER;
        } else {
            type = ITEM_WITHOUT_LETTER;
        }
        return type;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = mList.get(i).getSortLetter();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == sectionIndex) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return mList.get(position).getSortLetter().charAt(0);
    }

    private OnItemClickListener mOnItemClickListener;
    public interface OnItemClickListener {
        void OnItemClickListener(View view, int postion);
        void OnDelClickListener(int postion);
        void OnEditClickListener(int postion);
    }

    /**
     * 更新列表
     *
     * @param list
     */
    public void upDateList(List<BoxContact> list, boolean isSearch) {
        this.mList = list;
        this.isSearchType = isSearch;
        notifyDataSetChanged();
    }

    /**
     * 设置搜索框内容
     *
     * @param searchingString
     */
    public void setSearchingString(String searchingString) {
        this.searchingString = searchingString;
        //notifyDataSetChanged();
    }

    /**
     * 带索引的
     */
    class MyViewHolder1 extends RecyclerView.ViewHolder {
        private LinearLayout contactItemLayout;
        //首字母
        private TextView itemContactFirstLetter;
        //头像
        private TextView contactHeadImg;
        //名称
        private TextView contactNameTv;
        //卡号
        private TextView contactCardIdTv;


        public MyViewHolder1(View itemView) {
            super(itemView);
            contactItemLayout = itemView.findViewById(R.id.item_layout);
            itemContactFirstLetter = itemView.findViewById(R.id.item_contact_first_letter);
            contactHeadImg = itemView.findViewById(R.id.contact_head_tv);
            contactNameTv = itemView.findViewById(R.id.contact_name_tv);
            contactCardIdTv = itemView.findViewById(R.id.contact_id_tv);
        }
    }

    /**
     * 不带索引的
     */
    class MyViewHolder2 extends RecyclerView.ViewHolder {
        private LinearLayout contactItemLayout;
        //头像
        private TextView contactHeadImg;
        //名称
        private TextView contactNameTv;
        //卡号
        private TextView contactCardIdTv;

        public MyViewHolder2(View itemView) {
            super(itemView);
            contactItemLayout = itemView.findViewById(R.id.item_layout);
            contactHeadImg = itemView.findViewById(R.id.contact_head_tv);
            contactNameTv = itemView.findViewById(R.id.contact_name_tv);
            contactCardIdTv = itemView.findViewById(R.id.contact_id_tv);
        }
    }
}
